const { parkingsModel } = require('./models/parkingsModel')
const moment = require('moment')
// 新增车位

module.exports.addParking = async function (some) {
    const data = await parkingsModel.create(some)
    return data
}
// 获取车位数据与搜索订单功能
module.exports.getParkinData = async function ({ current, pageSize, value, input, state, lessorsId }) {
    console.log(lessorsId)
    console.log(current);
    switch (state) {
        case "未审核": state = '1'; break;
        case '审核通过': state = '2'; break;
        case '审核不通过': state = '3'; break;
        default: state = ''
    }
    // console.log(state, 3333);
    // console.log(current, pageSize, value, input, state, 3333333);

    // 获取车位列表数据
    if (lessorsId == undefined) {
        const data = await parkingsModel
            .find({ state: { $regex: state + '', $options: "$i" } })
            .find({ [value]: { $regex: input + '', $options: "$i" } })
            // 获取车位列表显示条数
            .limit(pageSize - 0)
            // 获取车位列表当前页数
            .skip((current - 1) * pageSize)
        // 车位列表的总条数
        const totle = await parkingsModel
            .find({ state: { $regex: state + '', $options: "$i" } })
            .find({ [value]: { $regex: input + '', $options: "$i" } })
            .countDocuments();
        // console.log(data);

        return { data, totle }
    } else {
        const data = await parkingsModel
            .find({ state: { $regex: state + '', $options: "$i" } })
            .find({ [value]: { $regex: input + '', $options: "$i" } })
            .find({ lessorsId })
            // 获取车位列表显示条数
            .limit(pageSize - 0)
            // 获取车位列表当前页数
            .skip((current - 1) * pageSize)
        // 车位列表的总条数
        const totle = await parkingsModel
            .find({ state: { $regex: state + '', $options: "$i" } })
            .find({ [value]: { $regex: input + '', $options: "$i" } })
            .find({ lessorsId })
            .countDocuments();
        // console.log(data);

        return { data, totle }
    }

}

// 获取多个车位状态的数据

module.exports.getTwoParkingState = async function ({ current, pageSize, value, input, state1, state2 }) {
    // console.log(current, pageSize, state1, state2, 333322333);
    // 获取车位审核列表数据
    const data = await parkingsModel
        .find({
            $or: [
                { state: state1 },
                { state: state2 }
            ]
        })
        .find({ [value]: { $regex: input, $options: "$i" } })
        // 获取车位列表显示条数
        .limit(pageSize - 0)
        // 获取车位列表当前页数
        .skip((current - 1) * pageSize)
    // 车位列表的总条数
    const totle = await parkingsModel
        .find({
            $or: [
                { state: state1 },
                { state: state2 }
            ]
        })
        .find({ [value]: { $regex: input, $options: "$i" } })
        .countDocuments();
    console.log(data, totle);
    return { data, totle }
}
// 删除车位

module.exports.deleteParking = async function (_id) {
    const data = await parkingsModel.deleteOne(_id)
    return data
}
// 获取需要修改车位的数据

module.exports.getUpdateParking = async function (_id) {
    console.log('持久层', _id);
    const data = await parkingsModel.find(_id)
    console.log('持久层返回数据', data);
    return data
}

// 传送修改车位的数据

module.exports.postUpdateParking = async function (some) {
    const data = await parkingsModel.updateOne({ _id: some._id }, some)
        .updateOne({ _id: some._id }, { time: moment().format('YYYY/MM/DD HH:mm') })

}
// 修改车位状态的数据
module.exports.updateParkingState = async function ({ _id, state }) {
    const data = await parkingsModel.updateOne({ _id }, { state: state })
        .updateOne({ _id }, { time: moment().format('YYYY/MM/DD HH:mm') });
    return data
}



